﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using SeleniumToolkit.Util;

namespace SeleniumToolkit.Config
{
    /// <summary>
    /// Collection of user extensions
    /// </summary>
    public class UserExtensionElementCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Gets the type of collection
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        /// <summary>
        /// Index property
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public UserExtensionElement this[int index]
        {
            get { return (UserExtensionElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Adds a user extension to the collection
        /// </summary>
        /// <param name="element"></param>
        public void Add(UserExtensionElement element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Creates a new user-extension element
        /// </summary>
        /// <returns></returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new UserExtensionElement();
        }

        /// <summary>
        /// Gets the key of the user extension element
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UserExtensionElement)element).Name;
        }

        /// <summary>
        /// Converts the defined user extensions into a single file to be included in the Selenium RC host
        /// </summary>
        /// <returns></returns>
        public string GetExtensionsFile()
        {
            if (_tempFile != null)
                return _tempFile;

            if (base.Count > 0)
            {   
                // Create new file for user extensions
                _tempFile = Path.Combine(Path.GetTempPath(), "user-extensions.js");

                // concatenate all the user extensions into a single file
                using (var appender = new FileAppender(_tempFile, "*.js"))
                {
                    foreach (UserExtensionElement element in this)
                    {
                        appender.Append(element.Path);
                    }
                }
            }

            return _tempFile;
        }

        /// <summary>
        /// Deletes the auto-generated user-extension file
        /// </summary>
        public void CleanupTempFile()
        {
            if (_tempFile != null && File.Exists(_tempFile))
                File.Delete(_tempFile);
            _tempFile = null;
        }

        private string _tempFile;
    }
}
